package daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import connector.Connector;
import daointerfaces.DALException;
import daointerfaces.ProduktBatchDAO;
import dto.ProduktBatchDTO;

public class MySQLProduktBatchDAO implements ProduktBatchDAO {

	@Override
	public ProduktBatchDTO getProduktBatch(int pbId) throws DALException {
		ResultSet rs = Connector.doQuery("SELECT * FROM produktBatchCDIO WHERE pbId = " + pbId);
		try {
			if (!rs.first()) throw new DALException("ProduktBatch " + pbId + " findes ikke");
			return new ProduktBatchDTO(rs.getInt(1), rs.getInt(2), rs.getInt(3),rs.getInt(4), rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getString(9));
		}
		catch (SQLException e) {throw new DALException(e); }
	}

	@Override
	public List<ProduktBatchDTO> getProduktBatchGroup(int pbId) throws DALException {
		List<ProduktBatchDTO> list = new ArrayList<ProduktBatchDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM produktBatchCDIO WHERE pbId = " + pbId);
		try
		{
			while (rs.next()) 
			{
				list.add(new ProduktBatchDTO(rs.getInt(1), rs.getInt(2), rs.getInt(3),rs.getInt(4), rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getString(9)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	public List<ProduktBatchDTO> getProduktBatchGroupBypbId() throws DALException {
		List<ProduktBatchDTO> list = new ArrayList<ProduktBatchDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM produktBatchCDIO GROUP BY pbId");
		try
		{
			while (rs.next())
			{
				list.add(new ProduktBatchDTO(rs.getInt(1), rs.getInt(2), rs.getInt(3),rs.getInt(4), rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getString(9)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}


	@Override
	public List<ProduktBatchDTO> getProduktBatchList() throws DALException {
		List<ProduktBatchDTO> list = new ArrayList<ProduktBatchDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM produktBatchCDIO");
		try
		{
			while (rs.next())
			{
				list.add(new ProduktBatchDTO(rs.getInt(1), rs.getInt(2), rs.getInt(3),rs.getInt(4), rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getString(9)));
			}
		}
		catch (SQLException e) { throw new DALException(e); }
		return list;
	}

	@Override
	public void createProduktBatch(ProduktBatchDTO produktbatch)
			throws DALException {
		Connector.doUpdate(
				"INSERT INTO produktBatchCDIO(pbId, receptId, status, oprId, rbId, tara, netto, startTid, slutTid) VALUES " +
						"(" + produktbatch.getPbId() + ", " + produktbatch.getReceptId() + ", " + produktbatch.getStatus() + ", " + produktbatch.getoprId()
						+ ", " + produktbatch.getrbId() + ", " + produktbatch.gettera() + ", " + produktbatch.getnetto() + ", '" + produktbatch.getstartTid() 
						+ "', '" + produktbatch.getslutTid() + "')"
				);
	}

	public void deleteProduktBatch(int pbId) throws DALException {
		Connector.doUpdate(
				"DELETE FROM produktBatchCDIO WHERE pbId=" + pbId
				);
	}

	@Override
	public void updateProduktBatch(ProduktBatchDTO produktbatch)
			throws DALException {
		Connector.doUpdate(
				"UPDATE produktBatchCDIO SET  pbId = " + produktbatch.getPbId() + ", receptId = " + produktbatch.getReceptId() + 
				", status =  " + produktbatch.getStatus() + ", oprId = " + produktbatch.getoprId() + ", rbId = " + produktbatch.getrbId() 
				+ ", tara = " + produktbatch.gettera() + ", netto = " + produktbatch.getnetto() +", startTid = '" + produktbatch.getstartTid() 
				+ "', slutTid = '"+ produktbatch.getslutTid()+
				"' WHERE pbId =  "  + produktbatch.getPbId() + " AND rbId = " + produktbatch.getrbId()
				);
	}

	public void updateProduktBatch_TimeStatusNettoOpr(ProduktBatchDTO produktbatch)
			throws DALException {
		Connector.doUpdate(
				"UPDATE produktBatchCDIO SET startTid = '" + produktbatch.getstartTid() + "', slutTid = '"+ produktbatch.getslutTid()+
				"', status =  " + produktbatch.getStatus() + ", oprId = " + produktbatch.getoprId()+ ", netto = " + produktbatch.getnetto() 
				+ " WHERE pbId =  "  + produktbatch.getPbId()
				);
	}
}
